import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { IView } from "../../mvc/interfaces/IView";
import { SnowmanModule } from "../../../modules/SnowmanModule";
import { DataEvent } from "../events/DataEvent";
import { SEntityId } from "../../../Message/Public/SEntityId";
import { GameProxy } from "../mvc/GameProxy";
import { IEntity } from "../scene/player/IEntity";
import { MonsterPlayer } from "../scene/player/entity/MonsterPlayer";
import { BossRule } from "../rules/BossRule";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { ItemData } from "../resource/info/ItemData";
import { EActiveType } from "../../../Message/Public/EActiveType";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { Alert } from "../../../com/mui/controls/Alert";
import { AIManager } from "../scene/ai/AIManager";
import { AIType } from "../scene/ai/AIType";
import { PointRule } from "../rules/PointRule";
import { TaskTargetInfo } from "../model/TaskTargetInfo";
import { EntityType } from "../rules/EntityType";
import { ISnowmanModule } from "../../../modules/interfaces/ISnowmanModule";
type int = number;
//class SnowmanController
    
    export  class SnowmanController extends Controller
    {
       

        protected /* override */ initServer(): void
        {
            super.initServer();
            Dispatcher.addEventListener(EventName.SnowballCommit, this.onSnowballCommitHandler);
            Dispatcher.addEventListener(EventName.Player_Selected, this.onPlayerSelected);
            Dispatcher.addEventListener(EventName.SnowmanBossClick, this.onSnowmanBossClickHandler);
            Dispatcher.addEventListener(EventName.SnowballItemUse, this.onSnowballItemUseHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackpackDataChange, this.onPackChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.onPackChangeHandler);
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._snowmanModule) 
            {
                this._snowmanModule = new SnowmanModule();
            }
            return this._snowmanModule;
        }

        /* internal  */onSnowballCommitHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["num"] as int;
            var loc2=arg1.data["bossEntityId"] as SEntityId;
            GameProxy.daily.decorateSnowman(loc1, loc2);
            return;
        }

        /* internal  */onPlayerSelected(arg1: DataEvent): void
        {
            var loc1=arg1.data as IEntity;
            var loc2=loc1 as MonsterPlayer;
            if (loc2 && BossRule.isFeedBoss(loc2.bossType)) 
            {
                loc1.addEventListener(PlayerEvent.UPDATEINFO, this.onBossInfoUpdate);
                if (loc2.selected) 
                {
                    view.show();
                    this._snowmanModule.bossEntityInfo = loc2.entityInfo.entityInfo;
                }
                else 
                {
                    view.hide();
                }
            }
            return;
        }

        /* internal  */onSnowmanBossClickHandler(arg1: DataEvent): void
        {
            view.show();
            return;
        }

        /* internal  */onSnowballItemUseHandler(arg1: DataEvent): void
        {
            var e: DataEvent;
            var itemData: ItemData;
            var onClickHandler: Function;

            var loc1;
            e = arg1;
            if (!cache.active.isActiveInEffect(EActiveType._EActiveTypeDecorateSnowman)) 
            {
                MsgManager.showRollTipsMsg(Language.getString(42431));
                return;
            }
            itemData = e.data as ItemData;
            if (itemData) 
            {
                onClickHandler = (arg1: int): void
                {
                    if (arg1 != Alert.OK) 
                    {
                        AIManager.onAIControl(_taskTargetInfo, AIType.AI_Convey);
                    }
                    else 
                    {
                        AIManager.onAutoPathAIControl(_taskTargetInfo);
                    }
                    initView();
                    PointRule.registNearPoint(_taskTargetInfo.mapId, _taskTargetInfo.x, _taskTargetInfo.y, _snowmanModule.show);
                    PointRule.registLeavePoint(_taskTargetInfo.mapId, _taskTargetInfo.x, _taskTargetInfo.y, _snowmanModule.hide);
                    Alert.resetToDefault();
                    return;
                }
                if (!this._taskTargetInfo) 
                {
                    this._taskTargetInfo = new TaskTargetInfo();
                    this._taskTargetInfo.mapId = 400101;
                    this._taskTargetInfo.x = 121;
                    this._taskTargetInfo.y = 282;
                    this._taskTargetInfo.targetType = EntityType.NPC;
                }
                Alert.okLabel = Language.getString(85002);
                Alert.calcelLabel = Language.getString(85003);
                Alert.buttonWidth = 70;
                Alert.show(Language.getString(42430), null, Alert.OK | Alert.CANCEL, null, onClickHandler);
            }
            return;
        }

        /* internal  */onBossInfoUpdate(arg1: PlayerEvent): void
        {
            var loc1=arg1.player;
            if (isViewShow) 
            {
                this._snowmanModule.bossEntityInfo = loc1.entityInfo.entityInfo;
            }
            return;
        }

        /* internal  */onPackChangeHandler(arg1: Object=null): void
        {
            if (isViewShow) 
            {
                this._snowmanModule.updatePropNum();
            }
            return;
        }

        private /* var */_snowmanModule: ISnowmanModule;

        private /* var */_taskTargetInfo: TaskTargetInfo;
    }
